package org.micode.libs.webflux;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;

import java.util.Objects;

public class WebExchangeHelper {
    public static final String LOCAL_IP = "127.0.0.1";
    private static final String UN_KNOWN = "unknown";

    public static String getRequestReferer(ServerWebExchange exchange) {
        String referer = exchange.getRequest().getQueryParams().getFirst("referer");
        referer = StringUtils.isAnyEmpty(referer)
                ? exchange.getRequest().getHeaders().getFirst("referer")
                : referer;
        return referer;
    }

    public static String getToken(ServerWebExchange exchange) {
        String token = getToken(exchange, "token");
        token = StringUtils.isAnyEmpty(token)
                ? getToken(exchange, "X-TOKEN")
                : token;
        return token;
    }

    public static String getToken(ServerWebExchange exchange, String key) {
        String token = exchange.getRequest().getQueryParams().getFirst(key);
        token = StringUtils.isAnyEmpty(token)
                ? exchange.getRequest().getHeaders().getFirst(key)
                : token;
        return token;
    }

    public static String getRemoteIp(ServerWebExchange exchange) {
        ServerHttpRequest request = exchange.getRequest();
        try {
            String realIp = request.getHeaders().getFirst("X-Real-IP");
            String forwardedIp = request.getHeaders().getFirst("X-Forwarded-For");
            if (!StringUtils.isEmpty(forwardedIp) && !UN_KNOWN.equalsIgnoreCase(forwardedIp)) {
                // 多次反向代理后会有多个ip值，第一个ip才是真实ip
                int index = forwardedIp.indexOf(",");
                if (index != -1) {
                    return forwardedIp.substring(0, index);
                } else {
                    return forwardedIp;
                }
            }
            forwardedIp = realIp;
            if (!StringUtils.isEmpty(forwardedIp) && !UN_KNOWN.equalsIgnoreCase(forwardedIp)) {
                return forwardedIp;
            }

            forwardedIp = readIpForHeader(request, "Proxy-Client-IP", forwardedIp);
            forwardedIp = readIpForHeader(request, "WL-Proxy-Client-IP", forwardedIp);
            forwardedIp = readIpForHeader(request, "HTTP_CLIENT_IP", forwardedIp);
            forwardedIp = readIpForHeader(request, "HTTP_X_FORWARDED_FOR", forwardedIp);

            if (StringUtils.isEmpty(forwardedIp) || UN_KNOWN.equalsIgnoreCase(forwardedIp)) {
                forwardedIp = Objects.requireNonNull(request.getRemoteAddress()).getAddress().getHostAddress();
            }

            return forwardedIp;
        } catch (Exception e) {
            return "未获取到IP";
        }
    }

    private static String readIpForHeader(ServerHttpRequest request,
                                          String fieldName, String currentIp) {
        if (StringUtils.isBlank(currentIp) || UN_KNOWN.equalsIgnoreCase(currentIp)) {
            return  request.getHeaders().getFirst(fieldName);
        } else {
            return currentIp;
        }
    }

}
